﻿<%@ WebHandler Language="C#" Class="ContainerReworkVue3PageHandler" %>

/* Description:返工返修
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan 
 * create Date:2024.8.19
 */

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BusinessRule.DTO;
using DatabaseAccess.DBUtility;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using System.Net;
using BusinessRule.Common;
using System.Linq;

public class ContainerReworkVue3PageHandler : IHttpHandler
{
    uLMSCommonIBusiness m_Common = uMESBusinessFactory.instance.GetuLMSCommonObject();
    uLMSContainerInStoreIBusiness m_ContainerData = uMESBusinessFactory.instance.GetuLMSContainerInStoreObject();

    uLMSReWorkIBusiness m_ReworkObj = uMESBusinessFactory.instance.GetuLMSReWorkObject();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (System.IO.StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                uMESProcessResult r = new uMESProcessResult();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getMaxStepReworkInfoNO":
                        r = getMaxStepReworkInfoNO(jObject);//获取返工编号
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "getHistoryReworkData":
                        r = getHistoryReworkData(jObject);//查询已返工记录
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "saveData":
                        r = saveData(jObject, context);//保存入库信息
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                uMESProcessResult r = new uMESProcessResult() { ReturnData = null, RowCount = "0", Code = "500", Message = e.Message };
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //获取返工编号
    uMESProcessResult getMaxStepReworkInfoNO(JObject obj)
    {
        DataTable dt = m_ReworkObj.GetMaxStepReworkInfoNO(obj["stepreworkinfono"].ToString());
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        return new uMESProcessResult() { ReturnData = dt, RowCount = dt.Rows.Count.ToString(), Code = "200", Message = "请求成功" };
    }

    //查询已返工记录
    uMESProcessResult getHistoryReworkData(JObject obj)
    {
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("ContainerID", obj["containerid"].ToString());
        para.Add("WorkflowStepID", obj["workflowstepid"].ToString());
        DataTable dt = m_ReworkObj.GetreWorkInfo(para);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        return new uMESProcessResult() { ReturnData = dt, RowCount = dt.Rows.Count.ToString(), Code = "200", Message = "请求成功" };
    }

    //保存入库信息
    uMESProcessResult saveData(JObject obj, HttpContext context)
    {
        DataTable dtReworkDataList = JsonConvert.DeserializeObject<DataTable>(obj["reworkDataList"].ToString());
        CurrentUserInfo m_UserInfo = m_Common.GetUserInfo(obj["employeename"].ToString(), "");
        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("ChangeBillNo", obj["changNo"].ToString());//更改单号
        para.Add("CheckEmployee", m_UserInfo.FullName);//检验员
        para.Add("CheckWorkCenter", obj["discoverDept"].ToString());//发现单位
        para.Add("ContainerID", obj["containerid"].ToString());
        para.Add("ContainerName", obj["containername"].ToString());
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("ProductID", obj["productid"].ToString());
        para.Add("ResponseWorkCenter", obj["dutyDept"].ToString());//责任单位
        para.Add("ResponseWorker", obj["dutyEmp"].ToString());//责任人
        para.Add("StepReworkInfoNO", obj["reworkNo"].ToString());//返修编号
        para.Add("StepReworkInfoID", Guid.NewGuid().ToString());
        para.Add("WorkflowStepID", obj["workflowstepid"].ToString());

        DataTable dt = new DataTable();
        dt.Columns.Add("ReWorkQty");
        dt.Columns.Add("ReWorkReason");
        foreach (DataRow dr in dtReworkDataList.Rows)
        {
            DataRow r = dt.NewRow();
            r["ReWorkQty"] = dr["reworkQty"].ToString();
            r["ReWorkReason"] = dr["reworkReason"].ToString();
            dt.Rows.Add(r);
        }
        if (m_ReworkObj.AddStepReworkInfo(para))
        {
            if (m_ReworkObj.AddStepReworkInfoDetail(dt, para["StepReworkInfoID"]))
            {
                m_ReworkObj.UpdateReWorkNum(para["ContainerID"], 1);
                return new uMESProcessResult() { ReturnData = true, RowCount = "0", Code = "200", Message = "添加返工、返修信息成功" };
            }
            else
            {
                return new uMESProcessResult() { ReturnData = false, RowCount = "0", Code = "200", Message = "添加返工、返修信息失败" };
            }
        }
        else
        {
            return new uMESProcessResult() { ReturnData = false, RowCount = "0", Code = "200", Message = "添加返工、返修信息失败" };
        }
    }
}